/** 
 * @file ppw
 * @brief PPW's Chitu Mind+ library.
 * @n This is a MindPlus graphics programming extension for PPW's ChiTu intelligent vehicle.
 * 
 * @copyright    PPW,2023.09
 * @copyright    MIT Lesser General Public License
 * 
 * @author [email](info@pinpinwa.cn)
 * @date  2022-09-22
*/


//% color="#E94709" iconWith=50 iconHeight=40
namespace chitu{

    //% block="ChiTu [RUN] Speed [SPEED]" blockType="command" 
    //% RUN.shadow="dropdown" RUN.options="RUN"
    //% SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=255 SPEED.defl=100

    export function ChiTu_Run(parameter: any, block: any) {
        let run = parameter.RUN.code;
        let speed = parameter.SPEED.code;

        Generator.addImport("from chitu import *"); 
        Generator.addInitHeader(`ChiTu`,`chitu = ChiTu()`); 

        Generator.addCode(`chitu.chituRun(chitu.${run},${speed})`);        
    }

    //% block="Chitu [MOTOR] Motor Run Speed [SPEED] Dir [DIR]" blockType="command" 
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR"
    //% SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=255 SPEED.defl=100
    //% DIR.shadow="dropdown" DIR.options="DIR"

    export function ChiTu_Motorset(parameter: any, block: any) {
        let motor = parameter.MOTOR.code;
        let speed = parameter.SPEED.code;
        let dir = parameter.DIR.code;

        Generator.addImport("from chitu import *"); 
        Generator.addInitHeader(`ChiTu`,`chitu = ChiTu()`); 

        Generator.addCode(`chitu.motorRun(chitu.${motor},chitu.${dir},${speed})`);  
   }

   //% block="ChiTu [MOTOR] Motor Stop" blockType="command"
   //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR"
   export function ChiTu_Motorstop(parameter:any, block:any) {
        let motor = parameter.MOTOR.code;

        Generator.addImport("from chitu import *"); 
        Generator.addInitHeader(`ChiTu`,`chitu = ChiTu()`); 


        Generator.addCode(`chitu.motorStop(chitu.${motor})`);
   }
    
   //% block="ChiTu Servo [SERVO] Angle [ANGLE]" blockType="command"
   //% SERVO.shadow="dropdown" SERVO.options="SERVO"
   //% ANGLE.shadow="range" ANGLE.params.min=0 ANGLE.params.max=180 ANGLE.defl=100
   export function ChiTu_Servoangle(parameter:any, block:any) {
    let servo = parameter.SERVO.code;
    let angle = parameter.ANGLE.code;

    Generator.addImport("from chitu import *"); 
    Generator.addInitHeader(`ChiTu`,`chitu = ChiTu()`); 

    Generator.addCode(`chitu.servoAngle(chitu.${servo},${angle})`);
    }

   //% block="ChiTu LED [TOGGLE]" blockType="command"
   //% TOGGLE.shadow="dropdown" TOGGLE.options="TOGGLE"
   export function ChiTu_LED(parameter:any, block:any) {
    let toggle = parameter.TOGGLE.code;  

    
    Generator.addImport("from chitu import *"); 
    Generator.addInitHeader(`ChiTu`,`chitu = ChiTu()`); 
    
    Generator.addCode(`chitu.toggleLED(chitu.${toggle})`);

    }

   //% block="ChiTu [SIDE] Detected [STATUS]" blockType="boolean"
   //% SIDE.shadow="dropdown" SIDE.options="SIDE"
   //% STATUS.shadow="dropdown" STATUS.options="STATUS"

   export function ChiTu_IRS(parameter:any, block:any) {
    let side = parameter.SIDE.code;
    let status = parameter.STATUS.code;

    Generator.addImport("from chitu import *"); 
    Generator.addInitHeader(`ChiTu`,`chitu = ChiTu()`); 
    Generator.addCode(`chitu.isSatus(chitu.${side},chitu.${status})`); 

   }

}